#ifndef hlua_h
#define hlua_h

#include <map>
#include "Engine.h"
#include "ReferenceCounted.h"
#include "lua.hpp"

namespace hxe
{
    class HLua : public virtual ReferenceCounted
    {
        friend class Engine;
    public:
        void dostring(const char* code);
        void update();
        void start();
        void destroy();

    private:
        lua_State* L;

        void openCustomLibs();

        void addSearcher();

        void callGlobalFunction(const char* funcName, int n, int r);


    private:
        std::map<int, ReferenceCounted*> objects;
        std::map<ReferenceCounted*, int> reverseMap;
        int cacheRef;

        void initRefCache();

    public:
        int addObject(ReferenceCounted* o);
        void removeObject(int i);
        void push(ReferenceCounted* o, int metaRef);
        ReferenceCounted* fastGetCObj(int index);

        static int gcObject(lua_State *L);
        static int objIndexer(lua_State *L);
        static int objNewIndexer(lua_State *L);

    private:
        HLua();
        ~HLua();
    };
}

#endif